<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script type="text/javascript" src="../layui/layui.js"></script>
    <script type="text/javascript" src="../javascript/jquery.min.js"></script>
    <script type="text/html" id="barCourse">
        <a class="layui-btn layui-btn-xs" id ="update" lay-event="selectCourse">选择</a>
      </script>
    <script type="text/html" id="barTeacher">
        <a class="layui-btn layui-btn-xs" id="update" lay-event="selectTeacher">选择</a>
    </script>

</head>
<!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>课程信息表</legend>
</fieldset> -->

<hr class="layui-bg-green">
<table class="layui-hide" id="Course" name="course" lay-filter="test" lay-size="sm" lay-even lay-skin="row"></table>
<!-- <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
    <legend>教师信息表</legend>
</fieldset> -->

<hr class="layui-bg-green">
<table class="layui-hide" id="Teacher" name="user" lay-filter="test" lay-size="sm"></table>
<hr class="layui-bg-green">

<div class="layui-form-item">
    <div class="layui-inline">
        <label class="layui-form-label">班级名</label>
        <div class="layui-input-inline">
            <input readonly="true" style="background:rgb(242, 244, 245)" type="tel" name="className" id="className"
                lay-verify="required|phone" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">课程名</label>
        <div class="layui-input-inline">
            <input readonly="true" style="background:rgb(242, 244, 245)" type="text" name="courseName" id="courseName"
                lay-verify="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">教师名</label>
        <div class="layui-input-inline">
            <input readonly="true" style="background:rgb(242, 244, 245)" type="text" name="" id="teacherName"
                lay-verify="email" autocomplete="off" class="layui-input">
        </div>
    </div>
    <a class="layui-btn layui-btn" id="addTeaching" lay-event="addTeaching" style="margin-left: 105px;">分配课程</a>
    <a class="layui-btn layui-btn" id="reset" lay-event="reset">重置选择</a>


</div>
<hr class="layui-bg-green">
<script>
    //Integer classId, Integer teacherId, Integer semId,String className, String teacherName

    $("#reset").click(function () {
        $("#courseName").val("")
        $("#teacherName").val("")
    });
    $("#addTeaching").click(function () {
        var a = $("#courseName").val()
        var b = $("#teacherName").val()
        if (a == null || a == "" || b == null || b == "") {
            layer.alert("课程和教师不能为空")
        }
        var url = "http://127.0.0.1:8088/arrange/add?className=" + classNames + "&techerId=" + teacherId
            + "&semeId=1"  + "&courseId=" + courseId + "&courseName=" + courseName
            + "&techerName=" + teacherName
        $.get(url, function (data, status) {
            var ret = JSON.parse(JSON.stringify(data));
            if (ret.status == 1) {
                layer.alert(ret.msg);
            } else {
                layer.alert(ret.msg);
                window.parent.location.reload();//刷新父页面
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭当前iframe页面
                parent.location.reload();//刷新父页面
            }

        });

    });

    var table;
    var classId;
    var classNames;

    var teacherId;
    var courseId;
    var teacherName;
    var courseName;
    layui.use(['table'], function () {
        classNames = window.location.href.split("=")[1];
       // className = window.location.href.split("=")[2];
        $("#className").val(decodeURIComponent(classNames))
        table = layui.table //表格
        table.render({
            elem: '#Course'
            , url: 'http://127.0.0.1:8088/course/findAll' //数据接口
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
            , height: 195
            , size : "sm"
            , title: '课程表'
            , page: true //开启分页
            , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }

                , { field: 'courseName', title: '课程名字', align: "center" }
                , { field: 'courseTime', title: '课程节数', align: "center" }
                , { field: 'statu', title: '安排教师数', align: "center" }
                , { fixed: 'right', align: 'center', toolbar: '#barCourse' }
            ]]
        });

        table.render({
            elem: '#Teacher'
            , url: 'http://127.0.0.1:8088/teacher/findAll' //数据接口
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                };
            }
            , title: '教师表'
            , size : "sm"

            , height: 195
            , page: true //开启分页
            , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }

                , { field: 'teacherName', title: '教师名字', align: "center" }
                , { field: 'teacherPhone', title: '电话号码', align: "center" }
                , { field: 'countLimit', title: '每周课程上限', align: "center" }
                , { field: 'statu', title: '课程数', align: "center" }
                , { fixed: 'right', align: 'center', toolbar: '#barTeacher' }
            ]]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'selectCourse') {
                selectCourse(data)
            } else if (layEvent == 'selectTeacher') {
                selectTeacher(data)
            }
        });
    });


    function selectCourse(data) {
        courseName = data.courseName
        courseId = data.id
        $("#courseName").val(courseName)
    }

    function selectTeacher(data) {
        teacherId = data.id
        teacherName = data.teacherName
        $("#teacherName").val(teacherName)
    }


    //重新加载table
    function reload() {
        table.reload('Course', {
            url: 'http://127.0.0.1:8088/course/findAll'
        })
    }
</script>

<body>
</body>

</html>